﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using AnyOffice.Components;
using AnyOffice.DAL;
using FunkeLab;
namespace AnyOffice.Control
{ 

    public partial class DepartmentControl : UserControl
    {
        protected MyTreeViewItem nodeParent;
        private string _target;
        private string _navigateUrl;
        private string toName;
        private string toId;
        private SqlConnection conn;
        private string no_whole_dept;

        public string NavigateUrl
        {
            get
            {
                return this._navigateUrl;
            }
            set
            {
                this._navigateUrl = value;
            }
        }
        public string Target
        {
            get
            {
                return this._target;
            }
            set
            {
                this._target = value;
            }
        }
        private void GetDept(SqlConnection conn, ref MyTreeViewItem nodeItem, int deptId)
        {
            DataTable table = new DepartmentDao().SelectChildAll(this.conn, deptId);
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    MyTreeViewItem item = new MyTreeViewItem();
                    item.Text = table.Rows[i]["DeptName"].ToString();
                    item.Link = this.NavigateUrl + "?dept_id=" + table.Rows[i]["DeptId"].ToString() + "&ToId=" + this.toId + "&ToName=" + this.toName + "&no_whole_dept=" + this.no_whole_dept;
                    item.TargetFrame = this.Target;
                    item.Image = "/images/node_dept.gif";
                    this.GetDept(this.conn, ref item, int.Parse(table.Rows[i]["DeptId"].ToString()));
                    nodeItem.Items.Add(item);
                }
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.toId = (Request.QueryString["ToId"] != null) ? Request.QueryString["ToId"].ToString() : "ToId";
                this.toName = (Request.QueryString["ToName"] != null) ? Request.QueryString["ToName"].ToString() : "ToName";
                this.no_whole_dept = (Request.QueryString["no_whole_dept"] != null) ? Request.QueryString["no_whole_dept"].ToString() : "";
                this.conn = new SqlConnection(ConfigManager.ConnectionString);
                DataTable table = new DepartmentDao().SelectParentAll(this.conn);
                if (table.Rows.Count > 0)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        this.nodeParent = new MyTreeViewItem();
                        this.nodeParent.Text = table.Rows[i]["DeptName"].ToString();
                        this.nodeParent.Link = this.NavigateUrl + "?dept_id=" + table.Rows[i]["DeptId"].ToString() + "&ToId=" + this.toId + "&ToName=" + this.toName + "&no_whole_dept=" + this.no_whole_dept;
                        this.nodeParent.TargetFrame = this.Target;
                        this.nodeParent.Image = "/images/node_dept.gif";
                        this.GetDept(this.conn, ref this.nodeParent, int.Parse(table.Rows[i]["DeptId"].ToString()));
                        this.TreeView1.Items.Add(this.nodeParent);
                    }
                }
                if (this.conn.State == ConnectionState.Open)
                {
                    this.conn.Close();
                }
            }
        }      

    }
}

